#include "test4.h"

/* 队列 */

void test4(void)
{
    // int data = 0;
    // sequence_t sequence;
    // InitSequence(&sequence);
    // InsertSequence(&sequence,22);
    // InsertSequence(&sequence,22);
    // InsertSequence(&sequence,22);
    // InsertSequence(&sequence,22);
    // InsertSequence(&sequence,22);

    // InsertLoopSequence(&sequence,22);
    // InsertLoopSequence(&sequence,22);
    // InsertLoopSequence(&sequence,22);
    // InsertLoopSequence(&sequence,22);
    // InsertLoopSequence(&sequence,22);
    // InsertLoopSequence(&sequence,22);

    // for(int i=0; i<Nu; i++){
    //     printf("sequence[%d]=%d\n",i,sequence.data[i]);
    // }
    // printf("front=%d  rear=%d\n",sequence.front,sequence.rear);

    // GetoutSequence(&sequence,&data);
    // GetoutSequence(&sequence,&data);
    // GetoutSequence(&sequence,&data);
    // GetoutLoopSequence(&sequence,&data);
    // GetoutLoopSequence(&sequence,&data);
    // GetoutLoopSequence(&sequence,&data);
    // GetoutLoopSequence(&sequence,&data);

    // for(int i=0; i<Nu; i++){
    //     printf("sequence[%d]=%d\n",i,sequence.data[i]);
    // }
    // printf("front=%d  rear=%d  data=%d\n",sequence.front,sequence.rear,data);

    // InsertLoopSequence(&sequence,22);
    // InsertLoopSequence(&sequence,22);
    // InsertLoopSequence(&sequence,22);

    // for(int i=0; i<Nu; i++){
    //     printf("sequence[%d]=%d\n",i,sequence.data[i]);
    // }
    // printf("front=%d  rear=%d\n",sequence.front,sequence.rear);

    // ArrangeSequence(&sequence);
    // for(int i=0; i<Nu; i++){
    //     printf("sequence[%d]=%d\n",i,sequence.data[i]);
    // }
    // printf("front=%d  rear=%d\n",sequence.front,sequence.rear);

    /* 链式队列 */
    int da = 0;
    linkqueue_t *linkqueue;
    linkqueue = CreatListSequence();
    InsertListSequence(linkqueue,55);
    InsertListSequence(linkqueue,22);
    InsertListSequence(linkqueue,66);
    PrintAllLinkqueue(linkqueue);

    printf("\n");
    GetOutListSequence(linkqueue, &da);
    PrintAllLinkqueue(linkqueue);
    printf("\n");
    printf("data = %d\n",da);
    
}

void InitSequence(sequence_t *sequence)
{
    for(int i = 0; i < Nu; i++){
        sequence->data[i]=0;
    }
    // 队列开始 头尾
    sequence->front=0;
    sequence->rear=0;
}

/*********************************************** 顺序队列 *************************************************/
int InsertSequence(sequence_t *sequence, data_t data)
{
    if( sequence->rear >= Nu ){
        printf("error\n");
        return err;
    }
    sequence->data[sequence->rear] = data;
    sequence->rear++;
    return right;
}

int GetoutSequence(sequence_t *sequence, data_t *data)
{
    if( sequence->front >= sequence->rear ){
        printf("error\n");
        return err;
    }
    *data = sequence->data[sequence->front];
    sequence->data[sequence->front]=0;
    sequence->front++;
    return right;
}

// 整理队列 将队头移动到1位置
int ArrangeSequence(sequence_t *sequence)
{
    int temp = 0;
    if( sequence->front >= sequence->rear ){
        sequence->front = 0;
        sequence->rear  = 0;
        return err;
    }
    if( sequence->front >= sequence->rear ){
        printf("error\n");
        return err;
    }
    temp = sequence->rear;
    for( int i=0; i<sequence->rear-sequence->front; i++){
        sequence->data[i] = sequence->data[sequence->front+i];
    }
    sequence->rear = sequence->rear-sequence->front;
    sequence->front = 0;
    for(int i=sequence->rear; i<temp; i++){
        sequence->data[i] = 0;
    }
}
/*********************************************** ******** *************************************************/
/*********************************************** 循环队列 *************************************************/
//空队列 front=rear  满队列 front-1=rear 
int InsertLoopSequence(sequence_t *sequence, int data)
{
    if(sequence->front-1 == sequence->rear%Nu || sequence->rear-sequence->front >= Nu){
        printf("error full\n");
        return err;
    }
    sequence->data[sequence->rear%Nu] = data;
    sequence->rear++;
}


int GetoutLoopSequence(sequence_t *sequence, int *data)
{
    if( sequence->front == sequence->rear){
        printf("error empty\n");
        return err;
    }
    *data = sequence->data[sequence->front];
    sequence->data[sequence->front] = 0;
    sequence->front ++;
}
/*********************************************** ******** *************************************************/
/*********************************************** 链式队列 *************************************************/
linkqueue_t *CreatListSequence(void)
{
    linkqueue_t *p;
    qnode_t *s;
    s = (qnode_t *)malloc(sizeof(qnode_t));
    s->data = 0;
    s->next = NULL;
    p = (linkqueue_t *)malloc(sizeof(linkqueue_t));
    p->front = s;
    p->rear  = s;
    p->len   = 0;
    return p;
}

int InsertListSequence(linkqueue_t *linkq, int data)
{
    qnode_t *s;
    s = (qnode_t *)malloc(sizeof(qnode_t));
    s->data = data;
    s->next = NULL;
    linkq->rear->next = s;
    linkq->rear = s;
    linkq->len++;
    return right;
}

int GetOutListSequence(linkqueue_t *linkq, int *data)
{
    qnode_t *s;
    if( linkq->len == 0 ){
        printf("error empty\n");
        return err;
    }
    s = linkq->front->next;
    *data = s->data;
    linkq->front->next = s->next;
    linkq->front->data = 0;
    free(s);
    linkq->len--;
    return right;
}
// 遍历打印
void PrintAllLinkqueue(linkqueue_t *linkq)
{
    qnode_t *p;
    p = linkq->front;
    for(int i=0; i<linkq->len+1; i++){
        printf("p[%d] = %d\n",i,p->data);
        p = p->next;
    }
}

/*********************************************** ******** *************************************************/
